package com.nanfang51g3.ckz.ui.share;

import com.nanfang51g3.ckz.di.ApiModule;
import com.nanfang51g3.ckz.domain.HttpResultBean;
import com.nanfang51g3.ckz.domain.ShareBean;
import com.nanfang51g3.ckz.exec.Api2Exception;
import com.nanfang51g3.ckz.http.HttpResultFunc;
import com.nanfang51g3.ckz.http.RestApi;
import com.nanfang51g3.ckz.http.SubscriberInterface;
import com.nanfang51g3.ckz.utils.KLog;
import com.nanfang51g3.ckz.utils.MD5Utils;
import com.nanfang51g3.ckz.utils.SPUtil;

import java.util.TreeMap;

import javax.inject.Inject;

import retrofit2.Retrofit;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/4/13.
 */
public class ShareTuiPresenter implements ShareTuiContract.ShareTuiActionsListener {

    private final RestApi _restApi;
    private final ShareTuiContract.View mView;

    @Inject
    public ShareTuiPresenter(RestApi restApi, ShareTuiContract.View mView) {
        KLog.i("TAG", "进入了...ShareTuiPresenter");
        /**
         * @see ApiModule#provideRestApi(Retrofit)
         */
        this._restApi = restApi;
        KLog.i("TAG", _restApi == null ? "进入了...is null" : "进入了...not null");
        /**
         * @see ShareTuiPresenterModule#ShareTuiPresenterModule(ShareTuiContract.View)
         */
        this.mView = mView;
        KLog.i("TAG", mView == null ? "进入了...is null" : "进入了...not null");
    }


    @Override
    public void requestUrl(String aid) {
        String userid = (String) SPUtil.get(mView.getContext(), "userid", "");
        TreeMap<String, String> treeMap = new TreeMap();
        treeMap.put("aid", aid);
        treeMap.put("userid", userid);
        String sign = MD5Utils.getAccessToken(treeMap);
        KLog.i("Evd", sign + "-" + treeMap.toString());
        Observable observable = _restApi.shareUrlView(aid, userid, sign);
        observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new ShareUrlResultFunc<ShareBean>())
                .subscribe(new SubscriberInterface<ShareBean>() {


                    /**
                     * 订阅开始时调用
                     */
                    @Override
                    public void onStart() {
                        super.onStart();
                        KLog.i("Evd", "onStart");
                    }

                    /**
                     * 请求完成
                     */
                    @Override
                    public void onCompleted() {
                        KLog.i("Evd", "onCompleted");

                    }

                    @Override
                    public void _onError(String msg) {
                        KLog.i("Evd", "Throwable " + msg);
                        /**
                         * @see ShareTuiActivity#success(boolean, String)
                         */
                        mView.success(false, null);
                    }


                    /**
                     * 数据
                     *
                     * @param o
                     */
                    @Override
                    public void onNext(ShareBean o) {
                        KLog.i("Evd", "onNext " + o.toString());
                        /**
                         * @see ShareTuiActivity#success(boolean, String)
                         */
                        mView.success(true, o.getUrl());
                    }
                });

    }

    class ShareUrlResultFunc<T> extends HttpResultFunc<T> {
        @Override
        public T call(HttpResultBean<T> httpResult) {
            KLog.i("Evd", httpResult == null ? "1-" : "0-" + httpResult.getCode() + "-" + httpResult.getInfo() + "-" + httpResult.getData().toString());
            if (httpResult.getCode() == 0) {
                throw new Api2Exception(httpResult.getInfo());
            }

            return httpResult.getData();
        }
    }
}
